package com.eascs.dubbo.loan.service;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.eascs.admin.common.service.IOverdueService;
import com.eascs.admin.common.vo.ModelResult;
import com.eascs.admin.common.vo.OverdueEntity;
import com.eascs.admin.common.vo.OverdueReportPO;
import com.eascs.admin.common.vo.RefundLine;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;

/**
 * 
 * @author yy
 *
 */
@Service
public class OverdueService implements IOverdueService{
	private final Logger logger = LoggerFactory.getLogger(OverdueService.class);
	@Autowired
	private O2oLoanapplyService o2oloanapplyService;
	@Override
	public  ModelResult<OverdueReportPO> queryOldOverdue(String idcard, String idType,String name,String phoneNumber) {
		// TODO Auto-generated method stub
		//检验参数
		 ModelResult<OverdueReportPO> result = new ModelResult<>("0000", "成功");
			if(StringUtil.isEmpty(idcard)){
				result.setErrCode("0001");
				result.setErrMsg("证件号码不能为空");
				return result;
			}
			if(StringUtil.isEmpty(idType)){
				result.setErrCode("0002");
				result.setErrMsg("证件类型不能为空");
				return result;
			}
			if(StringUtil.isEmpty(name)){
				result.setErrCode("0003");
				result.setErrMsg("用户名不能为空");
				return result;
			}
			if(StringUtil.isEmpty(name)){
				result.setErrCode("0004");
				result.setErrMsg("用户电话号码不能为空");
				return result;
			}
		logger.info("进入微信端根据身份证号获取逾期信息的方法，请求参数:", "idcard"+idcard+"idType"+idType);
		OverdueReportPO p = o2oloanapplyService.queryOldOverdue(idcard, idType);
		result.setModel(p);
		if(p!=null){
			logger.info("进微信端根据身份证号获取逾期信息的方法执行成功，请求参数:", "idcard"+idcard+"idType"+idType+"返回数据为："+JSON.toJSONString(p));
		}else{
			logger.info("进微信端根据身份证号获取逾期信息的方法执行成功，请求参数:", "idcard"+idcard+"idType"+idType+"为查到相关数据");
		}
		return result;
	}
	
	@Override
	public ModelResult<OverdueEntity> queryHKZ(String idcard, String idType,String name,String phoneNumber) {
		// TODO Auto-generated method stub
		//检验参数
		 ModelResult<OverdueEntity> result = new ModelResult<>("0000", "成功");
		if(StringUtil.isEmpty(idcard)){
			result.setErrCode("0001");
			result.setErrMsg("证件号码不能为空");
			return result;
		}
		if(StringUtil.isEmpty(idType)){
			result.setErrCode("0002");
			result.setErrMsg("证件类型不能为空");
			return result;
		}
		logger.info("进入微信端根据身份证号获取逾期信息的方法，请求参数:", "idcard"+idcard+"idType"+idType);
		
		List<RefundLine>  l = o2oloanapplyService.queryRefundLine(idcard, idType, name, phoneNumber);  //先查询还款计划表
		
		OverdueReportPO p = new OverdueReportPO();
		if(l.size()>0){
			p = o2oloanapplyService.queryOverdue(l.get(0).getBillCode());
			p.setHasData(Boolean.TRUE);
			if(p.getLoanAmount()== null || p.getLoanAmount().compareTo(BigDecimal.ZERO)==0){
				RefundLine r = l.get(0);
				p.setLoanAmount(r.getLoanAmount());
			}
			if(p.getLoanterm()==0){
				RefundLine r = l.get(0);
				p.setLoanterm(r.getLoanterm());
			}
			BigDecimal totalBack = new BigDecimal("0.0");
			for(int i=0;i<l.size();i++){
				RefundLine r = l.get(i);
				if(r.getArprincipal()!=null&&r.getArprincipal().compareTo(BigDecimal.ZERO)!=0){
					totalBack = totalBack.add(r.getArprincipal());
				}
				if(r.getArinterest()!=null&&r.getArinterest().compareTo(BigDecimal.ZERO)!=0){
					totalBack = totalBack.add(r.getArinterest());
				}
			}
			p.setTotalBack(totalBack); //未还本息
		}
		OverdueEntity oe = new OverdueEntity();
		oe.setOverdueReportPO(p);
		oe.setRefundLineList(l);
		result.setModel(oe);
		logger.info("进微信端根据身份证号获取逾期信息的方法执行成功，请求参数:", "idcard"+idcard+"idType"+idType+"返回数据为："+JSON.toJSONString(oe));
		return result;
	}
	
	
}
